package com.bawei.a2010douyin.widget;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Point;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.bawei.a2010douyin.R;

/**
 * 差值器：变化规律
 * 估值器：具体变化成值
 */
public class ViewActivity extends AppCompatActivity {
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);
        mImageView = findViewById(R.id.iv);
        int width = getWindowManager().getDefaultDisplay().getWidth();//屏幕宽度
        int height = getWindowManager().getDefaultDisplay().getHeight();//屏幕高度
        //TODO 2：动画
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new MyEvaluator(),
                new PointF(width / 2, height), new PointF(width / 2, 0));//估值器 起点 终点
        valueAnimator.setDuration(5000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {//中间变化的值
                PointF pointF = (PointF) animation.getAnimatedValue();
                Log.d("TAG", "pointF: "+pointF.x+"-"+pointF.y);
                mImageView.setX(pointF.x);
                mImageView.setY(pointF.y);
            }
        });
        valueAnimator.start();

    }
    //TODO 1：估值器
    class MyEvaluator implements TypeEvaluator<PointF>{
        private PointF point1 = new PointF(0,500);
        private PointF point2 = new PointF(1000,200);
        /**
         * @param t 0-1
         * @param point0 起始点
         * @param point3 结束点
         * @return 得到最终变化的点
         */
        @Override
        public PointF evaluate(float t, PointF point0, PointF point3) {
            PointF pointF = new PointF();
            pointF.x = point0.x*(1-t)*(1-t)*(1-t)
                    +3*point1.x*t*(1-t)*(1-t)
                    +3*point2.x*t*t*(1-t)
                    +point3.x*t*t*t;
            pointF.y = point0.y*(1-t)*(1-t)*(1-t)
                    +3*point1.y*t*(1-t)*(1-t)
                    +3*point2.y*t*t*(1-t)
                    +point3.y*t*t*t;

            return pointF;
        }
    }





}